package name.luojun.DBhelper.iface;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.log4j.Logger;
public abstract class DriverSupport implements IDriver {
	private Logger log = Logger.getLogger(this.getClass());
	protected Connection connection;
	public Connection releaseConnection() {
		Connection torelease = connection;
		connection = null;
		return torelease;
	}
	public void setConnection(Connection connection) {
		this.connection = connection;
	}
	public DriverSupport(Connection connection) {
		this.connection = connection;
	}
	protected void excute(String sql) {
		Statement statement = null;
		sql = sql.trim();
		if (sql.charAt(sql.length() - 1) == ';') {
			sql = sql.substring(0, sql.length() - 1);
		}
		try {
			statement = connection.createStatement();
			log.debug(sql);
			statement.execute(sql);
			statement.close();
		} catch (SQLException e) {
			log.error(e.getMessage() + ",SQL:" + sql);
		} finally {
			try {
				if (statement != null) {
					statement.close();
				}
			} catch (SQLException e) {
			}
		}
	}
	protected ResultSet excuteQuery(String sql) {
		try {
			Statement statement = connection.createStatement();
			log.debug(sql);
			return statement.executeQuery(sql);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
}
